package com.maoye.ossp.biz.intent.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.contract.entity.AssociatedContract;
import com.maoye.ossp.biz.contract.entity.Assshops;
import com.maoye.ossp.biz.contract.service.IContractService;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.intent.entity.IntAssterminate;
import com.maoye.ossp.biz.intent.service.IIntAssterminateService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 对象功能:联营撤柜意向  控制器类
 * 开发公司:MOI
 * 开发人员:zhongjt
 * 创建时间:2014-02-20 14:50:38
 */
@Controller
@RequestMapping("biz/intent/intAssterminate")
public class IntAssterminateController extends BaseController {
	@Resource
	private IIntAssterminateService intAssterminateService;
	@Resource
	private IContractService  contractServce;
	
	@RequestMapping(value = "list")
	public String list(IntAssterminate intAssterminate,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		QueryFilter queryFilter = getQuerFilter(request,intAssterminate);
		queryFilter.addFilter("areaId", intAssterminate.getAreaId(), QueryOP.EQUAL);
		queryFilter.addFilter("floorId", intAssterminate.getFloorId(), QueryOP.EQUAL);
		queryFilter.addFilter("storeId", intAssterminate.getStoreId(), QueryOP.EQUAL);
		queryFilter.addFilter("startDate", intAssterminate.getStartDate(), QueryOP.EQUAL);
		queryFilter.addFilter("endDate", intAssterminate.getEndDate(), QueryOP.EQUAL);
		queryFilter.addFilter("supplierName", intAssterminate.getSupplierName(), QueryOP.EQUAL);
		queryFilter.addFilter("supplierNo", intAssterminate.getSupplierNo(), QueryOP.EQUAL);
		
		if(intAssterminate.getIntentNo()!=null){
			queryFilter.addFilter("intentNo", intAssterminate.getIntentNo(), QueryOP.LIKE);
		}
		String createBy = null;
		if(StringUtil.isNotEmpty(intAssterminate.getCreateBy())){
			createBy = ContractUtils.getUserId(intAssterminate.getCreateBy());
		}
		queryFilter.addFilter("createBy", createBy, QueryOP.EQUAL);
		Page<IntAssterminate> page = (Page<IntAssterminate>) intAssterminateService.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("intAssterminate", intAssterminate);
		return resultPage("/biz/intent/intAssterminate/list", model, request);
	}

	@RequestMapping(value = "save")
	public String save(IntAssterminate intAssterminate, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intAssterminateService.save(intAssterminate);
		addMessage(redirectAttributes,
				"保存'" + intAssterminate.getSupplierName() + "撤柜意向'成功");
		return "redirect:/biz/intent/intAssterminate/list?navId="+navId;
	}
	
	
	@RequestMapping(value = "submit")
	public String submit(IntAssterminate intAssterminate, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intAssterminateService.submit(intAssterminate);
		addMessage(redirectAttributes,
				"提交'" + intAssterminate.getSupplierName() + "撤柜意向'成功");
		return "redirect:/biz/intent/intAssterminate/list?navId="+navId;
	}


	@RequestMapping(value = "{contractId}/add")
	public String add( @PathVariable Long contractId,Model model,HttpServletRequest request) {
		model = intAssterminateService.init(contractId,model);
		return resultPage("/biz/intent/intAssterminate/add", model, request);
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model,HttpServletRequest request) {
		IntAssterminate intAssterminate = intAssterminateService.get(id);
		AssociatedContract  associatedContract = contractServce.get(intAssterminate.getContractId().toString());
		model.addAttribute("intAssterminate", intAssterminate);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intAssterminate/add", model, request);
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id, Model model,HttpServletRequest request) {
		IntAssterminate intAssterminate = intAssterminateService.get(id);
		Long contractId = intAssterminate.getContractId();
		AssociatedContract  associatedContract = contractServce.get(contractId.toString());
		model.addAttribute("intAssterminate", intAssterminate);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intAssterminate/view", model, request);
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		intAssterminateService.remove(id);
		addMessage(redirectAttributes, "删除撤柜意向成功！");
		return "redirect:/biz/intent/intAssterminate/list";
	}
	/**
	 * 获取合同信息
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractList")
	public String getModuleContractList(HttpServletRequest request,HttpServletResponse response,Model model) {
		ArrayList<AssociatedContract> list =(ArrayList<AssociatedContract>) contractServce.getAssociatedContractList(null);
		model.addAttribute("contractList", list);
		return "/biz/intent/intAssdiscard/modal";
	}
	
	@RequestMapping(value = "{ids}/removeBatch")
	public String removeBatch(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) {
		String item = intAssterminateService.removeBatch(ids);
		if(!"".equals(item)){
			addMessage(redirectAttributes,"意向单号为"+item+"的撤柜意向已经进入流程审批，不能被删除！");
		}else{
			addMessage(redirectAttributes,"删除撤柜意向成功");
		}
		String navId = request.getParameter("navId");
		return "redirect:/biz/intent/intAssterminate/list?navId="+navId;
	}
}
